package addCompo.tabbar
{
	import flash.display.Graphics;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.utils.getDefinitionByName;
	
	import xm.definition.Align;
	
	
	/**
	 * <br> 클래스 설명 : </br>
	 * @author 강석민
	 */	
	public class XMTabBar extends MovieClip
	{
		//-----------------------------------------------------------------------------------
		// public vars
		//-----------------------------------------------------------------------------------
		public var bg : MovieClip;
		//-----------------------------------------------------------------------------------
		// protected / internal vars
		//-----------------------------------------------------------------------------------
		//-----------------------------------------------------------------------------------
		// private vars
		//-----------------------------------------------------------------------------------
		private var arrData : Array = [];
		private var tabBtnList : Vector.<MovieClip>;
		//-----------------------------------------------------------------------------------
		// public functions
		//-----------------------------------------------------------------------------------
		
		/**
		 * constructor 
		 */	
		public function XMTabBar()
		{
			trace("===> XMTabBar , XMTabBar : " );
			super();
			
			this.x = 0;
			this.y = stage.stageHeight - this.height;
			this.width = stage.stageWidth;
//			this.bg.width = stage.stageWidth;
			
			tabBtnList = new Vector.<MovieClip>();
		}
		
		[Inspectable(name="AddBtnClass", defaultValue="", type=Array )]
		public function set AddBtnClass ( value : Array ):void
		{
			arrData = value;
			makeBtn( value );
		}
		
		private function makeBtn( value : Array ):void
		{
			if( value.length > 0 ) this.bg.visible = false;
			
			//초기화
			tabBtnList.length = 0;
			var btnWidth : Number = this.bg.width/value.length;
			
			
			//
			var isClassDefined : Boolean = true;
			var makeClass : Class;
			var makeMovieClip : MovieClip;
			var len : int = value.length;
			var i : int = 0;
			while ( i < len )
			{
				//생성
				try
				{
					makeClass = getDefinitionByName( value[i].toString() ) as Class;
					isClassDefined = true;
				} 
				catch(error:Error) 
				{
					//정의 되지 않은 클래스 이름일때
					makeClass = getDefinitionByName("flash.display.MovieClip") as Class;
					isClassDefined = false;
				}
				makeMovieClip = new makeClass() as MovieClip;
				
				//정의 되지 않은 클래스 이름일때 메세지 표시
				if( isClassDefined == false )
				{
					var textFormat : TextFormat = new TextFormat();
					textFormat.align = Align.CENTER;
					var tf : TextField = new TextField();
					tf.defaultTextFormat = textFormat;
					tf.border = true;
					tf.width = btnWidth;
					tf.mouseEnabled = false;
					tf.selectable = false;
					tf.wordWrap = true;
					tf.autoSize = TextFieldAutoSize.CENTER;
					tf.text = value[i].toString();
					
					var sp : Shape = new Shape();
					var g : Graphics = sp.graphics;
					g.beginFill( 0xffffff, 0.8 );
					g.drawRect( 0, 0, btnWidth, tf.height );
					g.endFill();
					makeMovieClip.addChild( sp );
					makeMovieClip.addChild( tf );
					
				}
				
				tabBtnList.push( makeMovieClip );
				tabBtnList[i].gotoAndStop(1);
				tabBtnList[i].addEventListener(MouseEvent.MOUSE_DOWN , onBtnDown );
				tabBtnList[i].addEventListener(MouseEvent.MOUSE_UP , onBtnUp );
				tabBtnList[i].addEventListener(MouseEvent.MOUSE_OUT , onBtnUp );
				this.addChild( tabBtnList[i] );
				
				
				//버튼 넓이
				if( value.length > 0 ) 
				{
					tabBtnList[i].width = btnWidth;
				}
				
				//포지션
				if( i != 0 ) tabBtnList[i].x = btnWidth*i;
				
				++i;
			}
			
			this.y = stage.stageHeight - this.height;
		}
		
		
		[Inspectable(name="PaddingBottom", defaultValue=0, type=Number )]
		public function set PaddingBottom ( value : Number ):void
		{
			this.y = stage.stageHeight - this.height;
			this.y -= value;
		}
		
		[Inspectable(name="PaddingLeft", defaultValue=0, type=Number )]
		public function set PaddingLeft ( value : Number ):void
		{
			if( value < stage.stageWidth/2 )
			{
				this.x = value;
				this.width = stage.stageWidth - value*2;
			}else
			{
				throw 'PaddingLeft 값이 올바르지 않습니다.';
			}
		}
		
		//-----------------------------------------------------------------------------------
		// setter / getter
		//-----------------------------------------------------------------------------------
		//-----------------------------------------------------------------------------------
		// protected functions
		//-----------------------------------------------------------------------------------
		//-----------------------------------------------------------------------------------
		// internal functions
		//-----------------------------------------------------------------------------------
		//-----------------------------------------------------------------------------------
		// private functions
		//-----------------------------------------------------------------------------------
		//-----------------------------------------------------------------------------------
		// event handler
		//-----------------------------------------------------------------------------------
		
		protected function onBtnDown(e:MouseEvent):void
		{
			var currentTarget : MovieClip = e.currentTarget as MovieClip;
			currentTarget.gotoAndStop( 2 );
		}
		
		protected function onBtnUp(e:MouseEvent):void
		{
			var currentTarget : MovieClip = e.currentTarget as MovieClip;
			currentTarget.gotoAndStop( 1 );
		}
		
		
	}
}

